<?php
$backend_user_id = '';
$createBy = ($classModel->backend_user_id <> "") ? $classModel->backend_user_id : $classModel->users_id;

if (getLoginUserId() <> "") {
    $uid = getLoginUserId();
    $userDetail = Users::getFrontEndUser(getLoginUserId());
}
$email = (!empty($userDetail)) ? $userDetail->user->username : 'test@skillhippo.com';
$name = (!empty($userDetail)) ? $userDetail->first_name . ' ' . $userDetail->last_name : '';
?>

<section class="book_form make-query">
    <div class="container">
        <h4><?php echo Message::MAKE_QUERY;?></h4>
        <?php
        $form = $this->beginWidget('GxActiveForm', array(
            'id' => 'make-query-form',
            'action' => Yii::app()->createUrl('MakeQuery/Create'),
            'enableClientValidation' => true,
            'clientOptions' => array(
                'validateOnSubmit' => true,
                'validateOnChange' => true,
                'enableAjaxValidation' => true,
            ),
        ));
        ?>
        <div class="form-group clearfix">
            <label for="MakeQuery_name">Name <span class="required">*</span></label>
            <input name="MakeQuery[classes_id]" id="MakeQuery_classes_id" value="<?php echo $classModel->id; ?>" type="hidden" />
            <input name="MakeQuery[users_id]" id="MakeQuery_users_id" value="<?php echo getLoginUserId(); ?>" type="hidden" />
            <input name="MakeQuery[created_by]" id="MakeQuery_created_by" value="<?php echo $createBy; ?>" type="hidden" />
            <input name="MakeQuery[backend_user_id]" id="MakeQuery_created_by" value="<?php echo $backend_user_id; ?>" type="hidden" />
            <?php echo $form->textField($model, 'name', array('maxlength' => 45, 'class' => 'span2 form-control book-control', 'value' => $name, 'placeholder' => 'First Name Last Name')); ?>
            <?php echo $form->error($model, 'name'); ?>

        </div>

        <div class="form-group clearfix">

            <?php echo $form->hiddenField($model, 'email', array('maxlength' => 45, 'class' => 'field', 'readonly' => 'readonly', 'value' => $email, 'placeholder' => 'Email')); ?>
            <?php echo $form->error($model, 'email'); ?>
        </div>
        <div class="form-group clearfix">
            <label>Phone Number</label>
            <?php echo $form->textField($model, 'phone', array('maxlength' => 45, 'class' => 'span2 form-control book-control', 'placeholder' => 'Phone')); ?>
            <?php echo $form->error($model, 'phone'); ?>
        </div>
        <div class="form-group clearfix">
            <label>Time Slot<em title="Choose Time Slot for trainer to contact you" class="sprite question-sign">&nbsp;</em></label>
            <div id="timing"><select name="TimingMakequery[time_from][]" class="timeFrom"><?php numberofTimingsFrame(); ?></select>
                <span class="locate-arrow3"></span>
                <select name="TimingMakequery[time_to][]" class="timeTo"><?php numberofTimingsFrame(); ?></select>
                <span class="locate-arrow2"></span></div>
            <input type="hidden" name="numrow" id="numrow" value="1" />
            <span class="inline-block"><a href="javascript:void(0);" onclick="addRowTiming();" class="fontColor">+ Add additional time slots</a></span>
        </div>
        <div class="form-group clearfix">
            <label for="MakeQuery_query">Query <span class="required">*</span></label>
            <?php echo $form->textArea($model, 'query'); ?>
            <?php echo $form->error($model, 'query'); ?>
        </div>
        <div id="makequeryloader" style="display: none"><img src="<?php echo ThemePath(); ?>/images/myskill.gif"></img></div>
   
       <div id="makequeryresult" class="errorMessage"></div>
       <?php $closeClass=  ClassBatch::getClassClose($classModel->id);   ?>
 <?php
        if($seats>0 && $closeClass!='close'){
        echo CHtml::ajaxSubmitButton('Submit', CHtml::normalizeUrl(array('MakeQuery/create')), array(
            'dataType' => 'json',
            'type' => 'post',
            'success' => 'function(data) {
                        $("#makequeryloader").hide(); 
                        if(data.status == "nologin"){
                        window.location = "#top-position";
                        $("#login-panel").slideToggle();
                        return false;}
			else if(data.status=="sameuser"){						
                        $("#makequeryresult").html("You can not make query for own class.");}
                        if(data.status=="success"){                       
                        $("#makequeryresult").html("Your Query submitted successfully.");
                        } else{
                         $.each(data, function(key, val) {   
                        $("#"+key+"_em_").text(val);                                                    
                        $("#"+key+"_em_").show();                      
                        });
                        }

                    }',
            'beforeSend' => 'function(){                        
                           $("#makequeryloader").show();
                        checkTimingError();
                      }'
					  
        ), array('name' => 'makeQId', 'id' => 'makeQId'));}else{?>
       <input type="button" name="close" value="session close" class="closeclass">
        <?php }?>
      
        <?php
        $this->endWidget();
        ?>
    </div><!--form container-->
</section>
<script type="text/javascript">
                var rowNum = 0;
                function addRowTiming() {
                    rowNum = $("#numrow").val();
                    if (rowNum < 5) {
                        rowNum++;
                        var row = '<div id="rowNum' + rowNum + '"><select class="timeFrom' + rowNum + '" name="TimingMakequery[time_from][]"><?php numberofTimingsFrame(); ?></select> <span class="locate-arrow3"></span><select class="timeTo' + rowNum + '" name="TimingMakequery[time_to][]"><?php numberofTimingsFrame(); ?></select> <span class="locate-arrow2"></span><a href="javascript:void(0)"  onclick="removeRowTiming(' + rowNum + ');" class="remove-make-query">-Remove Time Slot</a></div>';
                        jQuery('#timing').append(row);
                        $("#numrow").val(rowNum);
                    }
                }
                function checkTimingError() {
                    if ($("#MakeQuery_phone").val() != "") {
                        $(".timefromclass").each(function() {
                            if (this.value == "") {
                                jQuery('#errorTiming').html('Please select time slot !');
                                return false;
                            } else {
                                jQuery('#errorTiming').html('');

                            }

                        });
                    }

                    return true;
                }

                function removeRowTiming(rowNum) {
                    jQuery('#rowNum' + rowNum).remove();
                    var numTextval = $("#numrow").val();
                    numTextval = numTextval - 1;
                    $("#numrow").val(numTextval);
                }

</script>